import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

export const routerArr = [{
		path: '',
		name: '',
		component: '',
		icon: 'icon-pingtai',
		meta: {
			title: '系统模块',
		},
		children: [{
			path: '/home',
			name: 'home',
			component: resolve => require(['@/pages/home/home'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '首页',
				keepAlive: false,
			}
		}, {
			path: '/setting',
			name: 'setting',
			component: resolve => require(['@/pages/sys/setting/setting'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '设置',
				keepAlive: false,
			}
		}]
	},
	{
		path: '',
		name: '',
		component: '',
		icon: 'icon-shangjia1',
		meta: {
			title: '商品管理',
			keepAlive: false,
		},
		children: [{
			path: '/goods',
			name: 'goods',
			component: resolve => require(['@/pages/goods/goods'], resolve),
			icon: 'icon-shangpin',
			meta: {
				title: '商品列表',
				keepAlive: false,
			},
		}]
	},
	{
		path: '',
		name: '',
		component: '',
		icon: 'icon-shangjia1',
		meta: {
			title: '其他模块',
			keepAlive: false,
		},
		children: [{
			path: '/earth',
			name: 'earth',
			component: resolve => require(['@/pages/others/earth/earth'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '3D地图',
				keepAlive: false,
			}
		}, {
			path: '/echarts',
			name: 'echarts',
			component: resolve => require(['@/pages/others/echarts/echarts'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: 'echarts',
				keepAlive: false,
			}
		}, {
			path: '/editor',
			name: 'editor',
			component: resolve => require(['@/pages/others/editor/editor'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '富文本编辑器',
				keepAlive: false,
			}
		}, {
			path: '/tree',
			name: 'tree',
			component: resolve => require(['@/pages/others/tree/tree'], resolve),
			icon: 'icon-dingdan',
			meta: {
				title: '树形列表',
				keepAlive: false,
			}

		}, {
			path: '/sheet',
			name: 'sheet',
			component: resolve => require(['@/pages/others/sheet/sheet'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '表单校验',
				keepAlive: false,
			}
		}, {
			path: '/upload',
			name: 'upload',
			component: resolve => require(['@/pages/others/upload/upload'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '文件上传',
				keepAlive: false,
			}
		}, {
			path: '/card',
			name: 'card',
			component: resolve => require(['@/pages/others/card/card'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '卡片',
				keepAlive: false,
			}
		}, {
			path: '/fold',
			name: 'fold',
			component: resolve => require(['@/pages/others/fold/fold'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '折叠面板',
				keepAlive: false,
			}
		}]
	}, {
		path: '',
		name: '',
		component: '',
		icon: 'icon-kuozhan',
		meta: {
			title: '杂项',
			keepAlive: false,
		},
		children: [{
			path: '/3d_gallery',
			name: '3d_gallery',
			component: resolve => require(['@/pages/special/3d_gallery/index'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: '3d_gallery',
				keepAlive: true,
			}

		}, {
			path: '/flower',
			name: 'flower',
			component: resolve => require(['@/pages/special/flower/index'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: 'flower',
				keepAlive: true,
			}

		}, {
			path: '/square',
			name: 'square',
			component: resolve => require(['@/pages/special/square/index'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: 'square',
				keepAlive: true,
			}
		}, {
			path: '/album',
			name: 'album',
			component: resolve => require(['@/pages/special/album/index'], resolve),
			icon: 'icon-pingtai2',
			meta: {
				title: 'album',
				keepAlive: false,
			}
		}]
	}
];

let childrenRouter = []; //二级路由
routerArr.forEach((x) => {
	if(x.children && x.children.length) {
		childrenRouter.push(...x.children);
	} else {
		childrenRouter.push(x);
	}
});

let routerList = new Router({
	routes: [{
			path: '/',
			component: resolve => require(['@/components/common/layout'], resolve),
			redirect: '/home', //重定向
			children: childrenRouter //二级路由
		},
		{
			path: '/login',
			component: resolve => require(['@/pages/login/login'], resolve)
		},
		{
			path: '/404',
			component: resolve => require(['@/pages/404/404'], resolve)
		},
		{
			path: '*', // 此处需特别注意至于最底部
			redirect: '/404'
		}
	],
});

//路由钩子
routerList.beforeEach((to, from, next) => {
	document.title = to.meta.title ? to.meta.title : '后台管理系统';
	let token = localStorage.getItem('token');
	if(token) {
		next();
	} else {
		if(to.path == '/login') {
			next();
		} else {
			next('/login');
		}
	}
});

export default routerList;